package com.kingtsoft.study.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.kingtsoft.study.common.JsonResult;
import com.kingtsoft.study.constant.DescribableEnum;
import com.kingtsoft.study.constant.ParamConstants;
import com.kingtsoft.study.model.StudyStudent;
import com.kingtsoft.study.model.StudyTeacher;
import com.kingtsoft.study.model.StudyUser;
import com.kingtsoft.study.persist.StudyStudentMapper;
import com.kingtsoft.study.persist.StudyTeacherMapper;
import com.kingtsoft.study.persist.StudyUserMapper;
import com.kingtsoft.study.service.StudyUserService;
import com.kingtsoft.study.util.JacksonUtil;
import com.kingtsoft.study.util.MD5;

/**
 * 
 * @ClassName: StudyUserService
 * @Description: 用户相关service实现类
 * @author: shenlx
 * @date: 2017年3月29日 上午8:51:12
 */
@Service
public class StudyUserServiceImpl implements StudyUserService {

	@Autowired
	private StudyUserMapper studyUserMapper;
	@Autowired
	private StudyStudentMapper studyStudentMapper;
	@Autowired
	private StudyTeacherMapper studyTeacherMapper;

	/**
	 * 
	 * @Title: doLogin
	 * @Description: 用户登录校验
	 * @author: shenlx
	 * @param studyUser
	 * @return: String
	 */
	@Override
	public String doLogin(StudyUser studyUser) {

		if (null == studyUser) {
			return JacksonUtil.bean2Json(new JsonResult(DescribableEnum.UNKNOWNUSER));
		} else {
			if (StringUtils.isBlank(studyUser.getUsername())) {
				return JacksonUtil.bean2Json(new JsonResult(DescribableEnum.UNKNOWNUSER));// 用户名不存在
			} else {
				StudyUser u = studyUserMapper.selectByUserName(studyUser.getUsername());
				if (null == u) {
					return JacksonUtil.bean2Json(new JsonResult(DescribableEnum.UNKNOWNUSER));// 用户名不存在
				} else {
					if (StringUtils.equals(MD5.getMD5(studyUser.getPassword()), u.getPassword())) {

						return JacksonUtil.bean2Json(new JsonResult(DescribableEnum.SUCCESS,u));

					} else {
						
						return JacksonUtil.bean2Json(new JsonResult(DescribableEnum.ERROR_PWD));// 密码错误
						
					}
				}
			}
		}

	}
	
	/**
	 * 
	 * @Title: selectStudentByUserId 
	 * @Description: 通过用户ID获取学生对象
	 * @author: shenlx
	 * @param userId
	 * @return
	 * @return: StudyStudent
	 */
	@Override
	public StudyStudent selectStudentByUserId(String userId) {
		return studyStudentMapper.selectByUserId(userId);
	}

	/**
	 * 
	 * @Title: selectTeacherByUserId 
	 * @Description: 通过用户ID获取老师对象
	 * @author: shenlx
	 * @param userId
	 * @return
	 * @return: StudyTeacher
	 */
	@Override
	public StudyTeacher selectTeacherByUserId(String userId) {
		return studyTeacherMapper.selectByUserId(userId);
	}

	/**
	 * 
	 * @Title: remoteUsername
	 * @Description: 校验用户名唯一性
	 * @author: shenlx
	 * @param username
	 * @return
	 * @return: boolean
	 */
	@Override
	public boolean remoteUsername(String username) {
		StudyUser u = studyUserMapper.selectByUserName(username);
		if (null != u) {// 用户已经存在
			return false;
		} else {
			return true;
		}

	}

	/**
	 * 
	 * @Title: addRegister
	 * @Description: 用户注册
	 * @author: shenlx
	 * @param studyUser
	 * @param studentName
	 *            这里为了方便，只做了学生注册，并只保存学生姓名
	 * @return
	 */
	@Override
	public void addRegister(StudyUser studyUser, String studentName) {
		studyUser.setUserType(ParamConstants.USER_TYPE_0);
		studyUser.setPassword(MD5.getMD5(studyUser.getPassword()));
		studyUserMapper.insert(studyUser);
		
		StudyStudent student = new StudyStudent();
		student.setUserId(studyUser.getId());
		student.setStudentName(studentName);
		studyStudentMapper.insert(student);
	}

}
